'
' *  Authors:  Benton Stark
' * 
' *  Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com) 
' *
' * Permission is hereby granted, free of charge, to any person obtaining a copy
' * of this software and associated documentation files (the "Software"), to deal
' * in the Software without restriction, including without limitation the rights
' * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' * copies of the Software, and to permit persons to whom the Software is
' * furnished to do so, subject to the following conditions:
' * 
' * The above copyright notice and this permission notice shall be included in
' * all copies or substantial portions of the Software.
' * 
' * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' * THE SOFTWARE.
' * 
' 


Imports System.Collections
Imports System.Collections.Generic
Imports System.Text


Namespace FTP
    ''' <summary>
    ''' Ftp response collection.
    ''' </summary>
    Public Class FtpResponseCollection
        Implements IEnumerable(Of FtpResponse)
        Private _list As New List(Of FtpResponse)()

        ''' <summary>
        ''' Default constructor.
        ''' </summary>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' Searches for the specified object and return the zero-based index of the
        ''' first occurrence within the entire FtpResponseCollection list.
        ''' </summary>
        ''' <param name="item">The FtpResponse object to locate in the collection.</param>
        ''' <return>The zero-based index of the first occurrence of item within the entire if found; otherwise, -1.</return>
        Public Function IndexOf(item As FtpResponse) As Integer
            Return _list.IndexOf(item)
        End Function



        ''' <summary>
        ''' Adds an FtpResponse to the end of the FtpResponseCollection list.
        ''' </summary>
        ''' <param name="item">FtpResponse object to add.</param>
        Public Sub Add(item As FtpResponse)
            _list.Add(item)
        End Sub

        ''' <summary>
        '''  Gets the number of elements actually contained in the FtpResponseCollection list.
        ''' </summary>
        Public ReadOnly Property Count() As Integer
            Get
                Return _list.Count
            End Get
        End Property

        Private Function GetEnumerator() As IEnumerator(Of FtpResponse) Implements IEnumerable(Of FtpResponse).GetEnumerator
            Return _list.GetEnumerator()
        End Function

        Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            Return _list.GetEnumerator()
        End Function


        ''' <summary>
        ''' Gets an FtpResponse from the FtpResponseCollection list based on index value.
        ''' </summary>
        ''' <param name="index">Numeric index of item to retrieve.</param>
        ''' <return>FtpResponse object.</return>
        Default Public ReadOnly Property Item(index As Integer) As FtpResponse
            Get
                Return _list(index)
            End Get
        End Property

        ''' <summary>
        ''' Remove all elements from the FtpResponseCollection list.
        ''' </summary>
        Public Sub Clear()
            _list.Clear()
        End Sub

        ''' <summary>
        ''' Get the raw FTP server supplied reponse text.
        ''' </summary>
        ''' <return>A string containing the FTP server response.</return>
        Public Function GetRawText() As String
            Dim builder As New StringBuilder()
            For Each item As FtpResponse In _list
                builder.Append(item.RawText)
                builder.Append(vbCr & vbLf)
            Next
            Return builder.ToString()
        End Function

        ''' <summary>
        ''' Get the last server response from the FtpResponseCollection list.
        ''' </summary>
        ''' <return>FtpResponse object.</return>
        Public Function GetLast() As FtpResponse
            If _list.Count = 0 Then
                Return New FtpResponse()
            Else
                Return _list(_list.Count - 1)
            End If
        End Function
    End Class
End Namespace
